{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Make The String Great"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #stack #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #栈 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: makeGood"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #整理字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个由大小写英文字母组成的字符串 <code>s</code> 。</p>\n",
    "\n",
    "<p>一个整理好的字符串中，两个相邻字符 <code>s[i]</code> 和 <code>s[i+1]</code>，其中 <code>0<= i <= s.length-2</code> ，要满足如下条件:</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>若 <code>s[i]</code> 是小写字符，则 <code>s[i+1]</code> 不可以是相同的大写字符。</li>\n",
    "\t<li>若 <code>s[i]</code> 是大写字符，则 <code>s[i+1]</code> 不可以是相同的小写字符。</li>\n",
    "</ul>\n",
    "\n",
    "<p>请你将字符串整理好，每次你都可以从字符串中选出满足上述条件的 <strong>两个相邻</strong> 字符并删除，直到字符串整理好为止。</p>\n",
    "\n",
    "<p>请返回整理好的 <strong>字符串</strong> 。题目保证在给出的约束条件下，测试样例对应的答案是唯一的。</p>\n",
    "\n",
    "<p><strong>注意：</strong>空字符串也属于整理好的字符串，尽管其中没有任何字符。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"leEeetcode\"\n",
    "<strong>输出：</strong>\"leetcode\"\n",
    "<strong>解释：</strong>无论你第一次选的是 i = 1 还是 i = 2，都会使 \"leEeetcode\" 缩减为 \"leetcode\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"abBAcC\"\n",
    "<strong>输出：</strong>\"\"\n",
    "<strong>解释：</strong>存在多种不同情况，但所有的情况都会导致相同的结果。例如：\n",
    "\"abBAcC\" --> \"aAcC\" --> \"cC\" --> \"\"\n",
    "\"abBAcC\" --> \"abBA\" --> \"aA\" --> \"\"\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>s = \"s\"\n",
    "<strong>输出：</strong>\"s\"\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= s.length <= 100</code></li>\n",
    "\t<li><code>s</code> 只包含小写和大写英文字母</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [make-the-string-great](https://leetcode.cn/problems/make-the-string-great/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [make-the-string-great](https://leetcode.cn/problems/make-the-string-great/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"leEeetcode\"', '\"abBAcC\"', '\"s\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for i in s:\n",
    "            if len(stack) != 0 and abs(ord(stack[-1])-ord(i)) == 32:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(i)\n",
    "        return \"\".join(stack)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for ch in s:\n",
    "            if stack and abs(ord(stack[-1]) - ord(ch)) ==  ord('a') - ord('A'):\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(ch)\n",
    "        return ''.join(stack)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack=[]\n",
    "        for char in s:\n",
    "            if stack and stack[-1].lower()==char.lower() and stack[-1]!=char:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(char)\n",
    "        return\"\".join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = ['dummy']\n",
    "        for c in s:\n",
    "            if stack[-1] == c.swapcase():\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(c)\n",
    "        return ''.join(stack[1:])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for ch in s:\n",
    "            if stack and abs(ord(stack[-1]) - ord(ch)) == 32:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(ch)\n",
    "        return ''.join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        new_s = s\n",
    "        \n",
    "        while self.delString(new_s):\n",
    "            new_s = new_s.replace(self.delString(new_s),'')\n",
    "        else:\n",
    "            return new_s\n",
    "\n",
    "    def delString(self,s):\n",
    "        l = len(s)\n",
    "        for i in range(l):\n",
    "            if i+1<l and s[i].lower() == s[i+1].lower():\n",
    "                if s[i] != s[i+1]:\n",
    "                    return s[i]+s[i+1]\n",
    "        else:\n",
    "            return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        st = []\n",
    "        for c in s:\n",
    "             if st and abs(ord(st[-1]) - ord(c)) == 32:\n",
    "                st.pop()\n",
    "                continue\n",
    "             st.append(c)\n",
    "        return ''.join(st) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for ch in s:\n",
    "            if stack and abs(ord(stack[-1]) - ord(ch)) == ord('a') - ord('A'):\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(ch)\n",
    "        return ''.join(stack)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack=[]\n",
    "        for i in s:\n",
    "            if not stack:\n",
    "                stack.append(i)\n",
    "            else:\n",
    "                top=stack[-1]\n",
    "                if top.isupper() and i.islower() and top.lower()==i:\n",
    "                    stack.pop()\n",
    "                elif top.islower() and i.isupper() and top.upper()==i:\n",
    "                    stack.pop()\n",
    "                else:\n",
    "                    stack.append(i)\n",
    "        return \"\".join([str(j) for j in stack])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = ['dummy']\n",
    "        for c in s:\n",
    "            if stack[-1] == c.swapcase():\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(c)\n",
    "        return ''.join(stack[1:])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        out = []\n",
    "        for i in s:\n",
    "            if not out:\n",
    "                out.append(i)\n",
    "            else:\n",
    "                if abs(ord(out[-1]) - ord(i)) == 32:\n",
    "                    out.pop()\n",
    "                else:\n",
    "                    out.append(i)\n",
    "        return ''.join(out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "      st = []\n",
    "      for ss in s:\n",
    "          if st and abs(ord(st[-1]) - ord(ss)) == 32:\n",
    "              st.pop()\n",
    "              continue\n",
    "          st.append(ss)\n",
    "      return ''.join(st)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        List = []\n",
    "        \n",
    "        \n",
    "        for ch in s:\n",
    "            if List and List[-1].lower() == ch.lower() and List[-1] != ch[-1]:\n",
    "                List.pop()\n",
    "            else:\n",
    "                List.append(ch)\n",
    "        return \"\".join(List)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for i in s:\n",
    "            if stack and ((ord(stack[-1])-32) == ord(i) or (ord(stack[-1])+32) == ord(i)):\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(i)\n",
    "\n",
    "        return ''.join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        ret = list()\n",
    "        for ch in s:\n",
    "            if ret and ret[-1] != ch and (ret[-1].lower() == ch or ret[-1] == ch.lower()):\n",
    "                ret.pop()\n",
    "            else:\n",
    "                ret.append(ch)\n",
    "        return \"\".join(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        res = [s[0],]\n",
    "        for char in s[1:]:\n",
    "            if res and ord(res[-1]) ^ ord(char) == 32:\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(char)\n",
    "        return \"\".join(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack=[]\n",
    "        for i in s:\n",
    "            if len(stack)>0 and i.lower()==stack[-1].lower() and i!=stack[-1]:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(i)\n",
    "        return ''.join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        tmp = []\n",
    "        for i in range(len(s)):\n",
    "            if len(tmp) == 0:\n",
    "                tmp.append(s[i])\n",
    "            elif abs(ord(tmp[-1]) - ord(s[i])) == 32:\n",
    "                tmp.pop()\n",
    "            else:\n",
    "                tmp.append(s[i])\n",
    "        res = ''\n",
    "        for t in tmp:\n",
    "            res += t\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for i in range(len(s)):\n",
    "            if stack and ord(stack[-1]) - 32 == ord(s[i]):\n",
    "                stack.pop()\n",
    "            elif stack and ord(stack[-1]) + 32 == ord(s[i]):\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(s[i])\n",
    "        return ''.join(stack)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        def bools(x,y):\n",
    "            if x.islower() and y.isupper() and x==y.lower():\n",
    "                return True\n",
    "            elif x.isupper() and y.islower() and y == x.lower():\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        def a(s):\n",
    "            i = 1\n",
    "            while i<len(s):\n",
    "                if bools(s[i-1],s[i]):\n",
    "                    del s[i-1]\n",
    "                    del s[i-1]\n",
    "                else:\n",
    "                    i = i+1\n",
    "        for i in range(len(s)):\n",
    "            a(s)\n",
    "        if len(s)==2 and bools(s[0],s[1]):\n",
    "            return \"\"\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        if len(s) < 2:\n",
    "            return s\n",
    "        x = []\n",
    "        for i in s:\n",
    "            if x and abs(ord(x[-1]) - ord(i)) == ord('a') - ord('A'):\n",
    "                x.pop()\n",
    "            else:\n",
    "                x.append(i)\n",
    "        return ''.join(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        n=len(s)\n",
    "        stack=[\"0\"]\n",
    "        stack.append(s[0])\n",
    "        for i in range(1,n):\n",
    "            if ord(stack[-1])<=122 and ord(stack[-1])>=97 and stack[-1].upper()==s[i]:\n",
    "                stack.pop()\n",
    "                continue\n",
    "            if ord(stack[-1])<=90 and ord(stack[-1])>=65 and stack[-1].lower()==s[i]:\n",
    "                stack.pop()\n",
    "                continue\n",
    "            else:\n",
    "                stack.append(s[i])\n",
    "        c=''.join(stack[1::])\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        a = \"\"\n",
    "        i = 0\n",
    "        index = 0\n",
    "        while(s != a):\n",
    "            if index == 1:\n",
    "                s = a\n",
    "                a = \"\"\n",
    "                i = 0\n",
    "            while i<len(s)-1:\n",
    "                if abs(ord(s[i]) - ord(s[i+1])) == 32:\n",
    "                    i += 2\n",
    "                else:\n",
    "                    a += s[i]\n",
    "                    i += 1\n",
    "            if(i == len(s)-1):\n",
    "                a += s[-1]\n",
    "            index =1\n",
    "        return a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for x in s:\n",
    "            if stack and abs(ord(x)-ord(stack[-1])) == 32:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(x)\n",
    "        return \"\".join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = [s[0]]\n",
    "        for i in s[1:]:\n",
    "            if stack and self.is_case_pair(stack[-1],i):\n",
    "                stack.pop()\n",
    "            else:stack.append(i)\n",
    "        return \"\".join(stack)\n",
    "\n",
    "\n",
    "    def is_case_pair(self,a,b):\n",
    "        return (a.isupper() and b.islower() and a==b.upper()) or ((b.isupper() and a.islower() and a==b.lower()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        ans = []\n",
    "        for ch in s:\n",
    "            if ans and abs(ord(ans[-1])-ord(ch)) == 32:\n",
    "                ans.pop()\n",
    "            else:\n",
    "                ans.append(ch)\n",
    "        return ''.join(ans)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for ch in s:\n",
    "            if len(stack):\n",
    "                if ch.islower() and ch.upper() == stack[-1] or ch.isupper() and ch.lower() == stack[-1]:\n",
    "                    stack.pop()\n",
    "                    continue\n",
    "            stack.append(ch)\n",
    "        \n",
    "        return \"\".join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        l=[]\n",
    "        for i in s:\n",
    "            l.append(i)\n",
    "            if len(l)>=2:\n",
    "                while len(l)>=2 and abs(ord(l[-1])-ord(l[-2]))==ord('a')-ord('A'):\n",
    "                    del l[-1]\n",
    "                    del l[-1]\n",
    "        ans=''\n",
    "        for i in l:\n",
    "            ans+=i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for ch in s:\n",
    "            if stack and abs(ord(stack[-1]) - ord(ch)) == 32:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(ch)\n",
    "        return ''.join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        st = []\n",
    "        for c in s:\n",
    "            if st and abs(ord(st[-1]) - ord(c)) == 32:\n",
    "                st.pop()\n",
    "                continue\n",
    "            st.append(c)\n",
    "        return ''.join(st) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        new_s = s\n",
    "        \n",
    "        while self.delString(new_s):\n",
    "            print(new_s)\n",
    "            new_s = new_s.replace(self.delString(new_s),'')\n",
    "            print(new_s,'---')\n",
    "        else:\n",
    "            print('0000')\n",
    "            return new_s\n",
    "\n",
    "    def delString(self,s):\n",
    "        l = len(s)\n",
    "        for i in range(l):\n",
    "            if i+1<l and s[i].lower() == s[i+1].lower():\n",
    "                if s[i] != s[i+1]:\n",
    "                    return s[i]+s[i+1]\n",
    "        else:\n",
    "            print('weizhao')\n",
    "            return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        if len(s)==1:\n",
    "            return s\n",
    "        result=s[0]\n",
    "        cur=1\n",
    "        while cur<len(s):\n",
    "            if len(result)>0 and abs(ord(s[cur])-ord(result[-1]))==32:\n",
    "                cur+=1\n",
    "                result=result[:-1]\n",
    "            else:\n",
    "                result+=s[cur]\n",
    "                cur+=1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for x in s:\n",
    "            if stack and ((stack[-1].islower() and x.isupper() and stack[-1].upper() == x) or (stack[-1].isupper() and x.islower() and stack[-1].lower() == x)):\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(x)\n",
    "        return ''.join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        while any(('a' <= s[i] <= 'z' and s[i].upper() == s[i+1]) or ('A' <= s[i] <= 'Z' and s[i].lower() == s[i+1]) for i in range(len(s)-1)):\n",
    "            for i in range(len(s)-1):\n",
    "                if ('a' <= s[i] <= 'z' and s[i].upper() == s[i+1]) or ('A' <= s[i] <= 'Z' and s[i].lower() == s[i+1]):\n",
    "                    s[i] = '#'\n",
    "                    s[i+1] = '#'\n",
    "            s = list(''.join(s).replace('#',''))\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []# 创建一个空列表\n",
    "        for ch in s:# 对于字符串s中的每个字符ch\n",
    "            if stack and abs(ord(stack[-1]) - ord(ch)) == 32:# 如果stack不为空且当前字符ch与栈顶元素的字符差异（以ASCII码的形式计算）等于32，即判断是否符合大小写\n",
    "                stack.pop()# 从stack中移除栈顶元素\n",
    "            else:\n",
    "                stack.append(ch)# 将当前字符ch添加到stack的末尾\n",
    "        return ''.join(stack)# 将stack中的所有字符连接起来并返回作为方法的返回值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        ret = list()\n",
    "        for ch in s:\n",
    "            if ret and ret[-1].lower() == ch.lower() and ret[-1] != ch:\n",
    "                ret.pop()\n",
    "            else:\n",
    "                ret.append(ch)\n",
    "        return \"\".join(ret)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for ch in s:\n",
    "            if stack and abs(ord(stack[-1])-ord(ch)) == 32:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(ch)\n",
    "        return ''.join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "            if len(s)==1:\n",
    "                return s\n",
    "            i = 0\n",
    "            while(True):\n",
    "                if i>len(s)-2:\n",
    "                    break\n",
    "                if s[i].lower() == s[i+1].lower():\n",
    "                    if s[i]!=s[i+1]:\n",
    "                        s = s[:i]+s[i+2:]+\"00\"\n",
    "                        i = 0\n",
    "                    else:\n",
    "                        i += 1\n",
    "                else:\n",
    "                    i+=1\n",
    "\n",
    "            s = s.replace(\"00\",\"\")\n",
    "            return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def dfs(self,s1,s2):\n",
    "        if ord('a')<=ord(s1)<=ord('z'):\n",
    "            return s1.upper()==s2\n",
    "        elif ord('A')<=ord(s1)<=ord('Z'):\n",
    "            return s1.lower()==s2\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for sym in s:\n",
    "            if stack == [] or not self.dfs(sym,stack[-1]):\n",
    "                stack.append(sym)\n",
    "            elif self.dfs(sym,stack[-1]):\n",
    "                stack.pop()\n",
    "        return ''.join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for ch in s:\n",
    "            if len(stack) >= 1 and ord(stack[-1])^ord(ch) == 32:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(ch)\n",
    "        return ''.join(stack)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        ret = list()\n",
    "        for ch in s:\n",
    "            if ret and ret[-1].lower() == ch.lower() and ret[-1] != ch:\n",
    "                ret.pop()\n",
    "            else:\n",
    "                ret.append(ch)\n",
    "        return \"\".join(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for ch in s:\n",
    "            if not stack:\n",
    "                stack.append(ch)\n",
    "                continue\n",
    "            while len(stack) >= 1:\n",
    "                if ch.isupper():\n",
    "                    if ch.lower() == stack[-1]:\n",
    "                        stack.pop()\n",
    "                    else:\n",
    "                        stack.append(ch)\n",
    "                    break\n",
    "                if ch.islower():\n",
    "                    if ch.upper() == stack[-1]:\n",
    "                        stack.pop()\n",
    "                    else:\n",
    "                        stack.append(ch)\n",
    "                    break\n",
    "        ans = ''.join(stack)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = ['']\n",
    "        for c in s:\n",
    "            if c != stack[-1] and str.upper(c) == str.upper(stack[-1]):\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(c)\n",
    "        return ''.join(stack)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        st=[]\n",
    "        for i in s:\n",
    "            if not st or (st[-1]!=i.lower() and st[-1]!=i.upper()) or st[-1]==i:\n",
    "                st.append(i)\n",
    "            else:\n",
    "                st.pop()\n",
    "        return ''.join(st)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        res=[]\n",
    "        for ch in s:\n",
    "            if res and res[-1]!=ch and (res[-1]==ch.lower() or res[-1]==ch.upper()):\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(ch)\n",
    "        return \"\".join(res)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        index, ans, l = 0, list(s), len(s) - 1\n",
    "        while index < l:\n",
    "            if index >= 0:\n",
    "                if abs(ord(ans[index]) - ord(ans[index + 1])) == 32:\n",
    "                    ans = ans[:index] + ans[index + 2:]\n",
    "                    l = len(ans) - 1\n",
    "                    index -= 2\n",
    "            index += 1\n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        #利用栈来做\n",
    "        #入栈数字和栈内字母比较，如果和栈顶ASCII数字相差不是32，入栈；否则栈顶数字出栈。\n",
    "        list_s = []\n",
    "        for i in s:\n",
    "            if list_s == []:\n",
    "                list_s.append(i)\n",
    "            else:\n",
    "                if abs(ord(list_s[-1]) - ord(i)) == 32:\n",
    "                    list_s.pop()\n",
    "                else:\n",
    "                    list_s.append(i)\n",
    "        return \"\".join(list_s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for ch in s:\n",
    "            if stack and abs(ord(stack[-1]) - ord(ch)) == ord('a') - ord('A'):\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(ch)\n",
    "        return ''.join(stack)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        n = len(s)\n",
    "        stack = [s[0]]\n",
    "        for i in range(1, n):\n",
    "            # if not len(stack):\n",
    "            #     stack.append(s[i])\n",
    "            if len(stack) and ((s[i].islower() and s[i].upper() ==  stack[-1])  or (s[i].isupper() and s[i].lower() == stack[-1])):\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(s[i])\n",
    "        return ''.join(stack)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack=[]\n",
    "        for i in s:\n",
    "            if len(stack)>0 and i.lower()==stack[-1].lower() and i!=stack[-1]:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(i)\n",
    "        return ''.join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for i in range(len(s)):\n",
    "            if stack and abs(ord(stack[-1])-ord(s[i])) == 32:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(s[i])\n",
    "        return \"\".join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        ret = list()\n",
    "        for ch in s:\n",
    "            if ret and ret[-1].lower() == ch.lower() and ret[-1] != ch:\n",
    "                ret.pop()\n",
    "            else:\n",
    "                ret.append(ch)\n",
    "        return \"\".join(ret)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        ret = []\n",
    "        for ch in s:\n",
    "            if ret and ret[-1].lower()==ch.lower() and ret[-1]!=ch:\n",
    "                ret.pop()\n",
    "            else:\n",
    "                ret.append(ch)\n",
    "        return ''.join(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for ch in s:\n",
    "            if stack and abs(ord(stack[-1])-ord(ch)) == 32:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(ch)\n",
    "        return ''.join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        ret = list()\n",
    "        for ch in s:\n",
    "            if ret and ret[-1].lower() == ch.lower() and ret[-1] != ch:\n",
    "                ret.pop()\n",
    "            else:\n",
    "                ret.append(ch)\n",
    "        return \"\".join(ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        res = [s[0],]\n",
    "\n",
    "        for char in s[1:]:\n",
    "            if len(res) >= 1 and ord(res[-1])^ord(char) == 32:\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(char)\n",
    "\n",
    "        return ''.join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        s_stack = []\n",
    "        for i in s:\n",
    "            if len(s_stack) == 0:\n",
    "                s_stack.append(i)\n",
    "            else:\n",
    "                top_char = s_stack[-1]\n",
    "                if top_char.islower() and i == top_char.upper():\n",
    "                    s_stack.pop()\n",
    "                elif top_char.isupper() and i == top_char.lower():\n",
    "                    s_stack.pop()\n",
    "                else:\n",
    "                    s_stack.append(i)\n",
    "        return \"\".join(s_stack)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        stack = []\n",
    "\n",
    "        for item in s:\n",
    "            print(stack)\n",
    "            if (stack and stack[-1].isupper() and item.islower() and stack[-1].lower() == item) or (stack and stack[-1].islower() and item.isupper() and stack[-1].upper() == item):\n",
    "                stack.pop()\n",
    "            else:\n",
    "                 stack.append(item)\n",
    "        return ''.join(stack)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        res = []\n",
    "        for i in s:\n",
    "            if res and res[-1].lower() == i.lower() and res[-1] != i:\n",
    "                res.pop()\n",
    "            else:\n",
    "                res.append(i)\n",
    "        return \"\".join(res)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        ret = list()\n",
    "        for ch in s:\n",
    "            if ret and ret[-1].lower() == ch.lower() and ret[-1] != ch:\n",
    "                ret.pop()\n",
    "            else:\n",
    "                ret.append(ch)\n",
    "        return \"\".join(ret)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        def bools(x,y):\n",
    "            if x.islower() and y.isupper() and x==y.lower():\n",
    "                return True\n",
    "            elif x.isupper() and y.islower() and y == x.lower():\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        def a(s):\n",
    "            i = 1\n",
    "            while i<len(s):\n",
    "                if bools(s[i-1],s[i]):\n",
    "                    del s[i-1]\n",
    "                    del s[i-1]\n",
    "                else:\n",
    "                    i = i+1\n",
    "        for i in range(int(len(s)/2)):\n",
    "            a(s)\n",
    "        if len(s)==2 and bools(s[0],s[1]):\n",
    "            return \"\"\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        index = -1\n",
    "        for each in s:\n",
    "            if index < 0:\n",
    "                index += 1\n",
    "                stack.append(each)\n",
    "            elif ((stack[index].islower() and each.isupper()) and (stack[index].upper() == each)) or ((stack[index].isupper() and each.islower()) and (stack[index].lower() == each)):\n",
    "                stack.pop()\n",
    "                index -= 1\n",
    "            else:\n",
    "                index += 1\n",
    "                stack.append(each)\n",
    "        return \"\".join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        sl=list(s)        \n",
    "        while True:\n",
    "            j=len(sl)\n",
    "            i=len(sl)-1\n",
    "            while i > 0 :\n",
    "                flag0=sl[i].lower()==sl[i-1].lower()\n",
    "                flag1=sl[i].islower() and sl[i-1].isupper()\n",
    "                flag2=sl[i].isupper() and sl[i-1].islower()\n",
    "                #print(sl[i],sl[i-1],flag0,flag1,flag2)\n",
    "                if flag0 and (flag1 or flag2):\n",
    "                    sl.pop(i)\n",
    "                    sl.pop(i-1)\n",
    "                    i-=2\n",
    "                i-=1\n",
    "            #print(sl)\n",
    "            if j==len(sl):\n",
    "                break\n",
    "        return ''.join(sl)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        s = list(s)\n",
    "        def bools(x,y):\n",
    "            if x.islower() and y.isupper() and x==y.lower():\n",
    "                return True\n",
    "            elif x.isupper() and y.islower() and y == x.lower():\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        def a(s):\n",
    "            i = 1\n",
    "            while i<len(s):\n",
    "                if bools(s[i-1],s[i]):\n",
    "                    del s[i-1]\n",
    "                    del s[i-1]\n",
    "                else:\n",
    "                    i = i+1\n",
    "        for i in range(int(len(s)/2)):\n",
    "            a(s)\n",
    "        if len(s)==2 and bools(s[0],s[1]):\n",
    "            return \"\"\n",
    "        return ''.join(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        st = []\n",
    "        for c in s:\n",
    "             if st and abs(ord(st[-1]) - ord(c)) == 32:\n",
    "                st.pop()\n",
    "                continue\n",
    "             st.append(c)\n",
    "        return ''.join(st) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for c in s:\n",
    "            if stack and (stack[-1].lower() == c.lower() and stack[-1] != c):\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(c)\n",
    "        return \"\".join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for c in s:\n",
    "            if stack and abs(ord(c) - ord(stack[-1])) == 32:\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(c)\n",
    "        return \"\".join(stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack=[]\n",
    "        for ch in s:\n",
    "            if not stack or abs(ord(stack[-1])-ord(ch))!=32:\n",
    "                stack.append(ch)\n",
    "            else:\n",
    "                stack.pop()\n",
    "        return\"\".join(ch for ch in stack)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        ans = [s[0]]\n",
    "        for c in s[1:]:\n",
    "            if ans and c.islower() and ans[-1].isupper() and c.upper() == ans[-1]:\n",
    "                ans.pop()\n",
    "            elif ans and c.isupper() and ans[-1].islower() and c.lower() == ans[-1]:\n",
    "                ans.pop()\n",
    "            else:\n",
    "                ans.append(c)\n",
    "        return \"\".join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for ch in s:\n",
    "            if stack and abs(ord(stack[-1]) - ord(ch)) == ord('a') - ord('A'):\n",
    "                stack.pop()\n",
    "            else:\n",
    "                stack.append(ch)\n",
    "        return ''.join(stack)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        stack = []\n",
    "        for c in s:\n",
    "            if not stack:\n",
    "                stack.append(c)\n",
    "            else:\n",
    "                if c >= 'a' and c <='z':\n",
    "                    if stack[-1] == c.upper():\n",
    "                        stack.pop()\n",
    "                    else:\n",
    "                        stack.append(c)\n",
    "                else:\n",
    "                    if stack[-1] == c.lower():\n",
    "                        stack.pop()\n",
    "                    else:\n",
    "                        stack.append(c)\n",
    "        return \"\".join(stack)\n",
    "                 \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        m=len(s)\n",
    "        a=[]\n",
    "        for i in range(m):\n",
    "            if a==[] or abs(ord(a[-1])-ord(s[i]))!=32:\n",
    "                a.append(s[i])\n",
    "                continue\n",
    "            elif abs(ord(a[-1])-ord(s[i]))==32:\n",
    "                a.pop()\n",
    "                \n",
    "                continue\n",
    "        return ''.join(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        ret = list()\n",
    "        for ch in s:\n",
    "            if ret and ret[-1].lower() == ch.lower() and ret[-1] != ch:\n",
    "                ret.pop()\n",
    "            else:\n",
    "                ret.append(ch)\n",
    "        return \"\".join(ret)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import deque\n",
    "\n",
    "class Solution:\n",
    "    def makeGood(self, s: str) -> str:\n",
    "        sl = [s[0]]\n",
    "        for i in range(1, len(s)):\n",
    "            if len(sl) > 0 and (s[i].islower() != sl[-1].islower()) and (s[i].lower() == sl[-1].lower()):\n",
    "                sl.pop()\n",
    "            else:\n",
    "                sl.append(s[i])\n",
    "        return ''.join(sl)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
